package com.yj_app.qualitytest.activity.scoredetail;

import android.os.Bundle;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.chad.library.adapter.base.entity.MultiItemEntity;
import com.yj_app.qualitytest.R;
import com.yj_app.qualitytest.activity.home.HomeActivity;
import com.yj_app.qualitytest.adapter.SeeDetailAdapter;
import com.yj_app.qualitytest.base.BaseActivity;
import com.yj_app.qualitytest.callback.UpRes;
import com.yj_app.qualitytest.info.BcInfo;
import com.yj_app.qualitytest.info.HdInfo;
import com.yj_app.qualitytest.info.SubScoreInfo;
import com.yj_app.qualitytest.info.Title2LInfo;
import com.yj_app.qualitytest.info.TitleInfo;

import org.litepal.LitePal;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Copyright (C), 2015-2019/9/18, 石家庄云捷软件科技有限公司
 *
 * @ProjectName: ScoreDetailActivity.java
 * @Package: com.yj_app.qualitytest.activity.scoredetail
 * @ClassName: ScoreDetailActivity
 * @Description: TODO
 * @Author: Hu_Sir
 * @CreateDate: 2019/9/18--11:22
 * @UpdateUser: ?
 * @UpdateDate: 2019/9/18--11:22
 * @UpdateRemark: todo
 */
public class ScoreDetailActivity extends BaseActivity {

    @BindView(R.id.my_recy)
    RecyclerView myRecy;
    @BindView(R.id.my_swf)
    SwipeRefreshLayout mySwf;
    SeeDetailAdapter seeDetailAdapter;
    private ArrayList<MultiItemEntity> list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_score_detail);
        ButterKnife.bind(this);
        initView();
        initEvent();
        getData();
    }

    private void initEvent() {
        up_load.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                toastSuccess("上传数据···");
//                LitePal.deleteAll(SubScoreInfo.class);
//                LitePal.deleteAll(BcInfo.class);
//                LitePal.deleteAll(HdInfo.class);
//                startActivity(HomeActivity.class);
//                finish();
                upLoadData(new UpRes() {
                    @Override
                    public void success() {
                        startActivity(HomeActivity.class);
                        finish();
                    }

                    @Override
                    public void error() {

                    }
                });



            }
        });
        mySwf.setOnRefreshListener(() -> {
            getData();
            mySwf.setRefreshing(false);
        });
    }

    private void getData() {
        list.clear();
        List<SubScoreInfo> subScoreInfoList = LitePal.findAll(SubScoreInfo.class);
        List<HdInfo> hdInfoList = LitePal.findAll(HdInfo.class);
        List<BcInfo> bcInfos = LitePal.findAll(BcInfo.class);
        addHd2Sub(hdInfoList,subScoreInfoList);
        addBc2Sub(bcInfos,subScoreInfoList);


        LinkedHashMap<String, LinkedHashMap<String, List<SubScoreInfo>>> map =
                groupListBy(subScoreInfoList);
        for (Map.Entry<String, LinkedHashMap<String, List<SubScoreInfo>>> entry : map.entrySet()) {
            TitleInfo queInfo = new TitleInfo();
            queInfo.setTitle(entry.getKey());
            LinkedHashMap<String, List<SubScoreInfo>> entryValue = entry.getValue();
            List<Title2LInfo> title2LInfos = new ArrayList<>();
            for (Map.Entry<String, List<SubScoreInfo>> entry2 : entryValue.entrySet()) {
                List<SubScoreInfo> scoreInfoList = entry2.getValue();
                double socre = 0;
                for (SubScoreInfo subScoreInfo : scoreInfoList) {
                    socre = socre + subScoreInfo.getScore();
                }
                Title2LInfo title2LInfo = new Title2LInfo();
                title2LInfo.setTitle(entry2.getKey());
                title2LInfo.setSubItems(scoreInfoList);
                title2LInfo.setKscore(socre);
                title2LInfos.add(title2LInfo);
            }
            queInfo.setSubItems(title2LInfos);
            list.add(queInfo);
        }
        seeDetailAdapter.setNewData(list);
        seeDetailAdapter.expandAll();
    }

    private void addBc2Sub(List<BcInfo> bcInfos, List<SubScoreInfo> subScoreInfoList) {
        for (BcInfo bcInfo:bcInfos){
            SubScoreInfo subScoreInfo =new SubScoreInfo();
            subScoreInfo.setCheckxmpname("标称");
            subScoreInfo.setCheckXmName(bcInfo.getItemname());
            subScoreInfo.setWorkspacename(bcInfo.getCjname());
            subScoreInfo.setSclinename(bcInfo.getScxname());
            subScoreInfo.setZrname(bcInfo.getNickname());
            subScoreInfo.setTeamname(bcInfo.getGcode());
            subScoreInfo.setCommon("次数："+bcInfo.getCount()+"、误差"+bcInfo.getWucha());
            subScoreInfoList.add(subScoreInfo);
        }
    }

    private void addHd2Sub(List<HdInfo> hdInfoList, List<SubScoreInfo> subScoreInfoList) {
        for (HdInfo hdInfo:hdInfoList){
            SubScoreInfo subScoreInfo =new SubScoreInfo();
            subScoreInfo.setCheckxmpname("厚度");
            subScoreInfo.setCheckXmName(hdInfo.getItemname());
            subScoreInfo.setWorkspacename(hdInfo.getCjname());
            subScoreInfo.setSclinename(hdInfo.getScxname());
            subScoreInfo.setZrname(hdInfo.getNickname());
            subScoreInfo.setTeamname(hdInfo.getGcode());
            subScoreInfo.setCommon("厚度"+hdInfo.getScore());
            subScoreInfoList.add(subScoreInfo);
        }
    }

    private LinkedHashMap<String, LinkedHashMap<String, List<SubScoreInfo>>> groupListBy(List<SubScoreInfo> list) {
        LinkedHashMap<String, LinkedHashMap<String, List<SubScoreInfo>>> pMap = new LinkedHashMap<>();
        for (SubScoreInfo bean : list) {
            if (pMap.containsKey(bean.getCheckxmpname())) {
                LinkedHashMap<String, List<SubScoreInfo>> hashMap = pMap.get(bean.getCheckxmpname());
                if (hashMap.containsKey(bean.getBzLxName() + bean.getWorkspacename() + bean.getSclinename() + bean.getZrname()
                        + bean.getTeamname() + bean.getMachineno() + bean.getBzfs())) {
                    List<SubScoreInfo> subScoreInfoList = hashMap.get(bean.getBzLxName() + bean.getWorkspacename() + bean.getSclinename() + bean.getZrname()
                            + bean.getTeamname() + bean.getMachineno() + bean.getBzfs());
                    subScoreInfoList.add(bean);
                } else {
                    List<SubScoreInfo> subList = new ArrayList<SubScoreInfo>();
                    subList.add(bean);
                    hashMap.put(bean.getBzLxName() + bean.getWorkspacename() + bean.getSclinename() + bean.getZrname()
                            + bean.getTeamname() + bean.getMachineno() + bean.getBzfs(), subList);

                }

            } else {
                LinkedHashMap<String, List<SubScoreInfo>> sMap = new LinkedHashMap<>();
                List<SubScoreInfo> subList = new ArrayList<SubScoreInfo>();
                subList.add(bean);
                sMap.put(bean.getBzLxName() + bean.getWorkspacename() + bean.getSclinename() + bean.getZrname()
                        + bean.getTeamname() + bean.getMachineno() + bean.getBzfs(), subList);
                pMap.put(bean.getCheckxmpname(), sMap);
            }
        }
        return pMap;

    }

    private LinkedHashMap<String, List<SubScoreInfo>> groupListBySeq(List<SubScoreInfo> list) {
        LinkedHashMap<String, List<SubScoreInfo>> map = new LinkedHashMap<String, List<SubScoreInfo>>();
        for (SubScoreInfo bean : list) {
            if (map.containsKey(bean.getBzLxName())) {
                List<SubScoreInfo> subList = map.get(bean.getBzLxName());
                subList.add(bean);
            } else {
                List<SubScoreInfo> subList = new ArrayList<SubScoreInfo>();
                subList.add(bean);
                map.put(bean.getBzLxName(), subList);
            }
        }
        return map;
    }

    private void initView() {
        setTitleWithBack("扣分总览");
        up_load.setVisibility(View.VISIBLE);
        list = new ArrayList<>();
        seeDetailAdapter = new SeeDetailAdapter(list);
        myRecy.setAdapter(seeDetailAdapter);
        myRecy.setLayoutManager(new LinearLayoutManager(this));
    }
}
